﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Data;
using BookStore.Bussiness;

namespace BookStore.admin
{
    public partial class Support : System.Web.UI.Page
    {
        static bool flag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["Administrator"])))
            {
                Response.Redirect("~/admin/Login.aspx");
            }
            showAll();
        }
        public void showAll()
        {
            gvSupport.DataSource = supportService.getAll();
            gvSupport.DataBind();
        }
        public void ResetControl() {
            txtsupportID.Text = "";
            txtNickname.Text = "";
            txtCaption.Text = "";
            txtPhone.Text = "";
        }
        public void insertSupport()
        {
            supportInfo objSupport = new supportInfo();
            objSupport.nickname = txtNickname.Text;
            objSupport.caption = txtCaption.Text;
            objSupport.phone = txtPhone.Text;
            bool test = supportService.insertSupport(objSupport);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        public void updateSupport()
        {
            supportInfo objSupport = new supportInfo();
            objSupport.supportID = Convert.ToInt32(txtsupportID.Text);
            objSupport.nickname = txtNickname.Text;
            objSupport.caption = txtCaption.Text;
            objSupport.phone = txtPhone.Text;
            bool test = supportService.updateSupport(objSupport);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('update success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('update failed');</script>");
            }
        }
        protected void lbtUpdateT_Click(object sender, EventArgs e)
        {
            if (flag==true)
            {
                insertSupport();
            }
            else
            {
                updateSupport();
            }
            showAll();
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void lbtBackT_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void lbtAddB_Click(object sender, EventArgs e)
        {
            flag = true;
            ResetControl();
            pnView.Visible = false;
            pnUpdate.Visible = true;
        }

        protected void lbtRefreshT_Click(object sender, EventArgs e)
        {
            showAll();
        }

        protected void gvSupport_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            flag = false;
            int supportID = Convert.ToInt32(gvSupport.Rows[e.NewSelectedIndex].Cells[0].Text);
            supportInfo objSupport = supportService.getbyID(supportID);
            txtsupportID.Text = objSupport.supportID.ToString();
            txtNickname.Text = objSupport.nickname;
            txtCaption.Text = objSupport.caption;
            txtPhone.Text = objSupport.phone;
            pnUpdate.Visible = true;
            pnView.Visible = false;
        }

        protected void gvSupport_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int supportID = Convert.ToInt32(gvSupport.Rows[e.RowIndex].Cells[0].Text);
            bool test = supportService.deleteSupport(supportID);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('delete success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('delete failed');</script>");
            }
            showAll();
        }

        protected void gvSupport_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSupport.PageIndex = e.NewPageIndex;
            showAll();
        }
    }
}
